package com.service.impl;

import com.dao.NewsDao;
import com.dao.NewsrecordDao;
import com.dto.DTO;
import com.dto.MessageEnum;
import com.entity.News;
import com.entity.Newsrecord;
import com.exception.SelectCourseFailException;
import com.service.NewsService;
import org.apache.ibatis.jdbc.Null;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
import java.util.Random;

@Service("NewsService")
public class NewsServiceImpl implements NewsService {
    @Resource
    NewsDao newsDao;
    @Resource
    NewsrecordDao newsrecordDao;

    @Override
    public DTO<News> getAllNewss()  throws Exception{
        List<News> newss = newsDao.getAllNewss();
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(newss).isPresent()?MessageEnum.ALLNEWSSUCCESS:MessageEnum.ALLNEWSFAIL;
        return new DTO<News>(messageEnum.getCode(),messageEnum.getMessage(),newss);
    }

//    @Override
//    public DTO<News> addNewsBynId(News news) {
//        Integer result = newsDao.addNewsBynId(news);
//        MessageEnum messageEnum  = result> 0?MessageEnum.ADDNEWSSUCCESS:MessageEnum.ADDNEWSFAIL;
//        return new DTO<News>(messageEnum.getCode(),messageEnum.getMessage());
//    }

    @Override
    public DTO<News> deleteNewsBynId(String nId)  throws Exception{
        Integer result = newsDao.deleteNewsBynId(nId);
        MessageEnum messageEnum  =result> 0?MessageEnum.DELETENEWSSUCCESS:MessageEnum.DELETENEWSFAIL;
        return new DTO<News>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<News> updateNewsBynId(News news)  throws Exception{
        Integer result = newsDao.updateNewsBynId(news);
        MessageEnum messageEnum  =result> 0?MessageEnum.UPDATENEWSSUCCESS:MessageEnum.UPDATENEWSFAIL;
        return new DTO<News>(messageEnum.getCode(),messageEnum.getMessage());
    }

//    @Override
//    public DTO<News> getNewsByPage(PageInfo pageInfo) {
//        Integer result = newsDao.getNewsByPage(PageInfo pageInfo);
//        MessageEnum messageEnum  =result> 0?MessageEnum.GETNEWSBYPAGESUCCESS:MessageEnum.GETNEWSBYPAGEFAIL;
//        return new DTO<News>(messageEnum.getCode(),messageEnum.getMessage());
//    }

    @Override
    public DTO<News> getNewsByIdWithAnos(String nId)  throws Exception{
        News news = newsDao.getNewsByIdWithAnos(nId);
        MessageEnum messageEnum  = Optional.ofNullable(news).isPresent()?MessageEnum.GETNEWBYIDWITHANOSSUCCESS:MessageEnum.GETNEWBYIDWITHANOFAIL;
        return new DTO<News>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<News> getNewsByIdWithAnoandmembers(String nId)  throws Exception{
        News news = newsDao.getNewsByIdWithAnoandmembers(nId);
        MessageEnum messageEnum  = Optional.ofNullable(news).isPresent()?MessageEnum.GETNEWBYIDWITHANOANDMEMBERSSUCCESS:MessageEnum.GETNEWBYIDWITHANOANDMEMBERSFAIL;
        return new DTO<News>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    @Transactional(rollbackFor = SelectCourseFailException.class)
    public DTO<News> addNews(News news) throws Exception {
        Integer result = newsDao.addNews(news);
//        if(Math.abs(new Random().nextInt()) % 2 == 0){
//            throw new SelectCourseFailException("增加新闻失败");
//        }
//        Newsrecord newsrecord = newsrecordDao.addNewsrecord();
        MessageEnum messageEnum  = result> 0?MessageEnum.ADDNEWSSUCCESS:MessageEnum.ADDNEWSFAIL;
        return new DTO<News>(messageEnum.getCode(),messageEnum.getMessage());
    }
}
